package maintain

import (
	"iot-base/common/logger"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
)

//UpgradeCmd 升级命令
type UpgradeCmd struct {
	URL     string
	Param   string
	EdgeIDs []string
}
//UpgradeByURL 升级
func UpgradeByURL(cmd UpgradeCmd) bool {
	url := cmd.URL
	param := cmd.Param
	return UpgradeByURLEx(url, param)
}
//UpgradeByURLEx 升级
func UpgradeByURLEx(url, param string) bool {

	file, _ := exec.LookPath(os.Args[0])
	path, _ := filepath.Abs(file)
	dir := filepath.Dir(path)
	if strings.Contains(strings.ToLower(url), "http") || strings.Contains(strings.ToLower(url), "ftp") {
		scriptpath := "sudo " + dir + "/upgradecurl.sh"
		logger.SLog.Info("run upgrade------------------------", scriptpath, url, param)
		//up, _ := body["param"]
		pCmd := exec.Command("/bin/sh", "-c", scriptpath+" "+url+" "+param)
		pCmd.Start()
		return true
	}
	logger.SLog.Error("run upgrade------------------------", url, param)
	return false
}
